/**
 * countdown.js
 * =======================================================
 */

(function(root, factory) {
  'use strict'

  if(typeof define === 'function' && define.amd) {
    define(['jquery'], factory)
  } else if(typeof module === 'object' && module.exports) {
    module.exports = function(root, jQuery) {
      if (jQuery === undefined) {
        if (typeof window !== 'undefined') {
          jQuery = require('jquery')
        } else {
          jQuery = require('jquery')(root)
        }
      }
      factory(jQuery)
      return jQuery
    }
  } else {
    factory(root.jQuery)
  }
}(this, function($) {
  'use strict'
  
  $.fn.countdown = function (message, duration, callback) {
    // If no message is provided, we use an empty string
    message = message || ""
    // Get reference to container, and set initial content
    var container = $(this[0]).html(duration + message)
    // Get reference to the interval doing the countdown
    var countdown = setInterval(function () {
        // If seconds remain
        if (--duration) {
            // Update our container's message
            container.html(duration + message)
        // Otherwise
        } else {
            // Clear the countdown interval
            clearInterval(countdown)
            // And fire the callback passing our container as `this`
            callback.call()
        }
    // Run interval every 1000ms (1 second)
    }, 1000)
  }
}));